// 获取环境变量（由 Vite 注入）
const ENV = import.meta.env.MODE || 'development';
const VERSION = import.meta.env.VITE_APP_VERSION || '1.0.0';
const BUILD_TIME = import.meta.env.VITE_BUILD_TIME || new Date().toISOString();
const COMMIT_SHA = import.meta.env.VITE_COMMIT_SHA || 'unknown';
const PIPELINE_ID = import.meta.env.VITE_PIPELINE_ID || 'unknown';

// 初始化页面
function initPage() {
    // 设置环境标识
    const envBadge = document.getElementById('env-badge');
    if (ENV === 'production') {
        envBadge.textContent = '🟢 生产环境 (Production)';
        envBadge.className = 'env-badge env-prod';
    } else if (ENV === 'test') {
        envBadge.textContent = '🟡 测试环境 (Test)';
        envBadge.className = 'env-badge env-test';
    } else {
        envBadge.textContent = '🔵 开发环境 (Development)';
        envBadge.className = 'env-badge';
        envBadge.style.background = '#d1ecf1';
        envBadge.style.color = '#0c5460';
    }

    // 设置版本号
    document.getElementById('version').textContent = VERSION;

    // 设置 Pipeline 信息
    document.getElementById('pipeline-id').textContent = `Pipeline ID: ${PIPELINE_ID}`;
    document.getElementById('commit-sha').textContent = `Commit SHA: ${COMMIT_SHA}`;
    document.getElementById('build-time').textContent = `构建时间: ${new Date(BUILD_TIME).toLocaleString('zh-CN')}`;

    console.log('🚀 应用已启动');
    console.log('环境:', ENV);
    console.log('版本:', VERSION);
    console.log('构建时间:', BUILD_TIME);
    console.log('Commit SHA:', COMMIT_SHA);
    console.log('Pipeline ID:', PIPELINE_ID);
}

// 显示构建信息
window.showBuildInfo = function() {
    const buildInfo = {
        environment: ENV,
        version: VERSION,
        buildTime: BUILD_TIME,
        commitSHA: COMMIT_SHA,
        pipelineId: PIPELINE_ID,
        userAgent: navigator.userAgent,
        timestamp: new Date().toISOString()
    };

    const resultDiv = document.getElementById('result');
    const buildInfoPre = document.getElementById('build-info');

    buildInfoPre.textContent = JSON.stringify(buildInfo, null, 2);
    resultDiv.style.display = 'block';

    // 滚动到结果区域
    resultDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
};

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', initPage);

// 添加一些交互效果
document.querySelectorAll('.tech-item').forEach(item => {
    item.addEventListener('click', function() {
        const name = this.querySelector('.name').textContent;
        alert(`✨ ${name} - 现代化前端工具链的重要组成部分！`);
    });
});

// 导出一些有用的函数
export function getEnvironment() {
    return ENV;
}

export function getVersion() {
    return VERSION;
}

export function getBuildInfo() {
    return {
        environment: ENV,
        version: VERSION,
        buildTime: BUILD_TIME,
        commitSHA: COMMIT_SHA,
        pipelineId: PIPELINE_ID
    };
}

